
package cn.javaxc.mvcwork.modules.file;
import cn.javaxc.mvcwork.framework.beans.TFormBean;
import cn.javaxc.model.base.*;
import cn.javaxc.mvcwork.util.helper.FrameHelper;
import cn.javaxc.mvcwork.util.common.DatabaseUtils;
import java.sql.*;
import java.util.ArrayList;




public class Thumbnail extends TFormBean {

  public final static String fs = System.getProperty("file.separator");

  private Object id = -1;
  private String filename = null;
  private int size = 0;
  private double version = 0;
  private java.sql.Timestamp entered = null;
  private int enteredBy = -1;
  private java.sql.Timestamp modified = null;
  private int modifiedBy = -1;


  
  public Thumbnail() {
  }


  
  public Thumbnail(SQLMap rs) throws SQLException {
    buildRecord(rs);
  }


  
  public void setId(Object tmp) {
    this.id = tmp;
  }



  
  public void setFilename(String tmp) {
    this.filename = tmp;
  }


  
  public void setSize(int tmp) {
    this.size = tmp;
  }


  
  public void setSize(String tmp) {
    this.size = Integer.parseInt(tmp);
  }


  
  public void setVersion(double tmp) {
    this.version = tmp;
  }

  public void setVersion(String tmp) {
    this.version = Double.parseDouble(tmp);
  }

  
  public void setEntered(java.sql.Timestamp tmp) {
    this.entered = tmp;
  }


  
  public void setEntered(String tmp) {
    this.entered = DatabaseUtils.parseTimestamp(tmp);
  }


  
  public void setEnteredBy(int tmp) {
    this.enteredBy = tmp;
  }


  
  public void setEnteredBy(String tmp) {
    this.enteredBy = Integer.parseInt(tmp);
  }


  
  public void setModified(java.sql.Timestamp tmp) {
    this.modified = tmp;
  }


  
  public void setModified(String tmp) {
    this.modified = DatabaseUtils.parseTimestamp(tmp);
  }


  
  public void setModifiedBy(int tmp) {
    this.modifiedBy = tmp;
  }


  
  public void setModifiedBy(String tmp) {
    this.modifiedBy = Integer.parseInt(tmp);
  }


  
  public Object getId() {
    return id;
  }


  
  public String getFilename() {
    return filename;
  }


  
  public int getSize() {
    return size;
  }


  
  public double getVersion() {
    return version;
  }


  
  public java.sql.Timestamp getEntered() {
    return entered;
  }


  
  public int getEnteredBy() {
    return enteredBy;
  }


  
  public java.sql.Timestamp getModified() {
    return modified;
  }


  
  public int getModifiedBy() {
    return modifiedBy;
  }


  
  public int getRelativeSize() {
    int newSize = (size / 1000);
    if (newSize == 0) {
      return 1;
    } else {
      return newSize;
    }
  }

  
  public boolean insert() throws SQLException {
    SQLMap pst = FrameHelper.getBaseSQL().createStatement(
        "INSERT INTO ts_files_thumbnail " +
            "(item_id, filename, version, size, " +
            "entered, modified, " +
            "enteredBy, modifiedBy) " +
            "VALUES (?, ?, ?, ?, " +
            (entered != null ? "?" : "CURRENT_TIMESTAMP")+", " +
            (modified != null ? "?" : "CURRENT_TIMESTAMP")+", " +
            "?, ?) ");
    int i = 0;
    pst.setObject(++i, id);
    pst.setString(++i, filename);
    pst.setDouble(++i, version);
    pst.setInt(++i, size);
    if (entered != null) {
      pst.setTimestamp(++i, entered);
    }
    if (modified != null) {
      pst.setTimestamp(++i, modified);
    }
    pst.setInt(++i, enteredBy);
    pst.setInt(++i, modifiedBy);
    pst.execute(null);
    
    return true;
  }


  
  public boolean delete(String baseFilePath) throws SQLException {
    //Need to delete the actual file
    String filePath = baseFilePath + FileItem.getDatePath(this.getEntered()) + this.getFilename();
    java.io.File fileToDelete = new java.io.File(filePath);
    if (!fileToDelete.delete()) {
      System.err.println("Thumbnail-> Tried to delete file: " + filePath);
    }
    //Delete database record
    String sql =
        "DELETE FROM ts_files_thumbnail " +
            "WHERE item_id = ? " +
            "AND version = ? ";
    SQLMap pst = FrameHelper.getBaseSQL().createStatement(sql);
    pst.setObject(1, this.getId());
    pst.setDouble(2, this.getVersion());
    pst.execute(null);
    return true;
  }


  
  protected void buildRecord(SQLMap rs) throws SQLException {
    id = rs.getInt("item_id");
    filename = rs.getString("filename");
    size = rs.getInt("size");
    version = rs.getDouble("version");
    entered = rs.getTimestamp("entered");
    enteredBy = rs.getInt("enteredby");
    modified = rs.getTimestamp("modified");
    modifiedBy = rs.getInt("modifiedby");
  }


  
  public static ArrayList recordList() throws SQLException {
    ArrayList records = new ArrayList();
    SQLMap pst = FrameHelper.getBaseSQL().createStatement(
        "SELECT * FROM ts_files_thumbnail " +
            "WHERE item_id > -1 ");
    SQLList<SQLMap> elist = pst.executeQuery(null,null);
	for (SQLMap rs:elist ){
      Thumbnail thumbnail = new Thumbnail(rs);
      records.add(thumbnail);
    }
    return records;
  }
}
